<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Opex extends MX_Controller {
  function __construct() {
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->model('outlet/mopex');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
  }
	public function add_new($id = 0, $pesan = "hal"){
    $this->template->title('Sistem', "Opex Edit");
    if($id > 0){
      $data_detail = $this->global_models->get("m_opex", array("id_opex" => $id));
//      $this->debug($data_detail, true);
    }
    else{
      $data_detail[0]->id_opex = 0;
      $data_detail[0]->name = "";
    }
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('add-new-opex', 
        array('message' => $pesan,
              'url'     => base_url()."themes/srabon/",
              'title'   => 'Add Opex',
              'detail'  => $data_detail,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
              'foot'    => '
                <script>
                  $(function () {
                    // validate signup form on keyup and submit
                    var validator = $("#formoutlet").validate({
                        rules: {
                            name: "required"
                        },
                        messages: {
                            name: "Enter name"
                        }
                    });
                });
                </script>
                '
            ));
      $this->template
        ->set_layout('default')
        ->build('add-new-opex');
    }
    else{
      if($this->input->post("id_detail", TRUE)){
        $kirim = array(
              "name"                  =>  $this->input->post("name", TRUE),
              "update_by_users"       =>  $this->session->userdata('id')
          );
        $hasil_in = $this->global_models->update("m_opex", array("id_opex" => $this->input->post("id_detail", TRUE)), $kirim);
        if($hasil_in){
          redirect ("outlet/opex/index/list/sukses");
        }
        else
          redirect ("outlet/opex/index/list/filed");
      }
      else{
        $pst = $this->input->post(NULL, TRUE);
//        $pst = $pst['addressform']['addressform'][0];
        if($pst['name']){
          $kirim = array(
              "name"                  =>  $pst['name'],
              "create_by_users"       =>  $this->session->userdata('id'),
              "create_date"           =>  date("Y-m-d H:i:s"),
              "update_by_users"       =>  $this->session->userdata('id')
          );
          if($this->global_models->insert("m_opex", $kirim))
            redirect ("outlet/opex/index/list/sukses");
          else{
            redirect ("outlet/opex/index/list/filed");
          }
        }
        else{
          redirect ("outlet/opex/index/list/filed");
        }
      }
    }
  }
	public function delete($id){
    if($this->global_models->delete("m_opex", array("id_opex" => $id)))
      redirect("outlet/opex/index/list/sukses");
    else
      redirect("outlet/opex/index/list/filed");
  }
	public function export_xls(){
    $this->musers->export_xls("data-users");
  }
	public function index($action = "list", $pesan = "hal", $hal = 0){
//    $this->debug($this->menu, true);
    $list = $this->global_models->get("m_opex");
    $this->template->title('Sistem', "Opex");
    
    $this->template->build('main-opex', 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'data'        => $list,
            'menu_drop'   => $this->menu,
            'menu'        => "settings",
            'title_table' => "Opex Data Table",
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build('main-opex');
	}
  public function opex_addrow(){
    $no = rand(100, 100000);
    $list_pend = $this->global_models->get_dropdown("m_opex", "id_opex", "name", TRUE);
    $isi = $this->form_eksternal->form_dropdown("opex_outlet[]", $list_pend, array());
    print <<<EOD
    <tr id="tr_{$no}"><td>{$isi}</td>
   <td><a href='#' class='remove-element' onclick="del_row_tambahan('tr_{$no}')" title='Remove' >Remove</a></td></tr>
EOD;
    die;
  }
  public function opex_outlet($id){
    $detail = $this->global_models->get("m_outlet", array("id_outlet" => $id));
    $notin = $this->global_models->get_array("id_opex", "d_opex_outlet", array("id_outlet" => $detail[0]->id_outlet));
    $list_pend = $this->global_models->get_dropdown("m_opex", "id_opex", "name", TRUE);
    $tr[] = "tr_1";
    if(is_array($notin)){
      $r = 2;
      foreach($notin as $vl){
        $tr[] = "tr_".$r++;
      }
    }
//    $this->debug($notin, true);
    if(!$this->input->post(NULL, TRUE)){
      $this->template->build('opex', 
        array('url'     => base_url()."themes/srabon/",
              'title'   => 'Opex Outlet',
              'title_form'   => 'Opex untuk '.$detail[0]->name,
              'menu_drop'   => $this->menu,
              'menu'        => "settings",
              'foot'    => '',
              'list_pend' =>  $list_pend,
              'sel' =>  $notin,
              'tr' =>  $tr,
            ));
      $this->template
        ->set_layout('default')
        ->build('opex');
    }
    else{
      $pst = $this->input->post(NULL, TRUE);
      if($pst['opex_outlet']){
        if(is_array($pst['opex_outlet'])){
          foreach ($pst['opex_outlet'] as $plo) {
            if($plo != 0)
              $kirim[] = $plo;
          }
        }
        if($this->mopex->insert_opex_outlet($detail[0]->id_outlet, $kirim))
          redirect ("outlet/milik/list/sukses");
        else
          redirect ("outlet/milik/list/filed");
      }
    }
  }
  function sign_opex(){
    $outlet = $this->global_models->get("m_outlet");
    foreach ($outlet as $value) {
      $this->mopex->insert_opex_outlet($value->id_outlet, array(1, 3, 4, 5, 7, 8, 9, 10));
    }
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */